package com.swift.utils;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;

import com.swift.base.ApplicationBase;

import java.util.ArrayList;
import java.util.List;

public class ActivityUtil {

    volatile static ActivityUtil instances;

    List<Activity> pageList = new ArrayList<Activity>();
    List<Activity> picSelectList = new ArrayList<Activity>();


    public static ActivityUtil getInstances() {
        if (instances == null) {
            synchronized (ActivityUtil.class) {
                if (instances == null) {
                    instances = new ActivityUtil();
                }
            }
        }
        return instances;
    }

    /**
     * @throws
     * @author: HYM
     * @Title: add
     * @Description: TODO(添加一个activity)
     * @param: @param activity
     * @return: void
     */
    public void add(Activity activity) {
        if (!pageList.contains(activity)) {
            pageList.add(activity);
        }
    }

    /**
     * @throws
     * @author: HYM
     * @Title: exit
     * @Description: TODO(finish全部的activity list)
     * @param:
     * @return: void
     */
    public void exit() {
        for (Activity activity : pageList) {
            activity.finish();
        }
    }

    /**
     * @throws
     * @author: HYM
     * @Title: add
     * @Description: TODO(添加一个activity)
     * @param: @param activity
     * @return: void
     */
    public void addPicSelect(Activity activity) {
        if (!picSelectList.contains(activity)) {
            picSelectList.add(activity);
        }
    }

    /**
     * @throws
     * @author: HYM
     * @Title: exit
     * @Description: TODO(finish全部的activity list)
     * @param:
     * @return: void
     */
    public void exitPicSelect() {
        for (Activity activity : picSelectList) {
            activity.finish();
        }
    }

    /**
     * @throws
     * @author: HYM
     * @Title: start
     * @Description: TODO(开启一个新的类)
     * @param: @param clazz
     * @return: void
     */
    public void start(Class clazz) {
        Intent start = new Intent(ApplicationBase.Context, clazz);
        start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ApplicationBase.Context.startActivity(start);
    }

    /**
     * @desc Android support library 23.4.0: android.support.v7.widget.TintContextWrapper cannot be cast to Activity
     * @author hym
     * @createDate 2018/1/22 16:11
     */
    public Activity getRequiredActivity(View req_view) {
        Context context = req_view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

    /**
    * @desc
    * @author hym
    * @createDate 2018/2/1 14:49
    */
    public void animationActivity(Activity a,View transitionView, String transitionName, Intent intent)
    {
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(a, transitionView, transitionName);
        ActivityCompat.startActivity(a,intent, options.toBundle());
    }
}
